.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1989 AT&T
.\" Portions Copyright (c) 1992, X/Open Company Limited  All Rights Reserved
.\" Copyright (c) 2002, Sun Microsystems, Inc.  All Rights Reserved.
.\"
.TH TRUNCATE 3C "Apr 5, 2002"
.SH NAME
truncate, ftruncate \- set a file to a specified length
.SH SYNOPSIS
.LP
.nf
#include <unistd.h>

\fBint\fR \fBtruncate\fR(\fBconst char *\fR\fIpath\fR, \fBoff_t\fR \fIlength\fR);
.fi

.LP
.nf
\fBint\fR \fBftruncate\fR(\fBint\fR \fIfildes\fR, \fBoff_t\fR \fIlength\fR);
.fi

.SH DESCRIPTION
.sp
.LP
The \fBtruncate()\fR function causes the regular file named by \fIpath\fR to
have a size equal to \fIlength\fR bytes.
.sp
.LP
If the file previously was larger than \fIlength\fR, the extra data is
discarded. If the file was previously shorter than length, its size is
increased, and the extended area appears as if it were zero-filled.
.sp
.LP
The application must ensure that the process has write permission for the file.
.sp
.LP
This function does not modify the file offset for any open file descriptions
associated with the file.
.sp
.LP
The \fBftruncate()\fR function causes the regular file referenced by
\fIfildes\fR to be truncated to \fIlength\fR. If the size of the file
previously exceeded \fIlength\fR, the extra data is no longer available to
reads on the file. If the file previously was smaller than this size,
\fBftruncate()\fR increases the size of the file with the extended area
appearing as if it were zero-filled. The value of the seek pointer is not
modified by a call to \fBftruncate()\fR.
.sp
.LP
The \fBftruncate()\fR function works only with regular files and shared memory.
If \fIfildes\fR refers to a shared memory object, \fBftruncate()\fR sets the
size of the shared memory object to \fIlength\fR. If \fIfildes\fR refers to a
directory or is not a valid file descriptor open for writing, \fBftruncate()\fR
fails.
.sp
.LP
If the effect of \fBftruncate()\fR is to decrease the size of a shared memory
object or memory mapped file and whole pages beyond the new end were previously
mapped, then the whole pages beyond the new end shall be discarded.
.sp
.LP
If the effect of \fBftruncate()\fR is to increase the size of a shared memory
object, it is unspecified if the contents of any mapped pages between the old
end-of-file and the new are flushed to the underlying object.
.sp
.LP
These functions do not modify the file offset for any open file descriptions
associated with the file.  On successful completion, if the file size is
changed, these functions will mark for update the \fBst_ctime\fR and
\fBst_mtime\fR fields of the file, and if the file is a regular file, the
\fBS_ISUID\fR and \fBS_ISGID\fR bits of the file mode are left unchanged.
.sp
.LP
If the request would cause the file size to exceed the soft file size limit for
the process, the request will fail and a \fBSIGXFSZ\fR signal will be generated
for the process.
.SH RETURN VALUES
.sp
.LP
Upon successful completion, \fBftruncate()\fR and \fBtruncate()\fR return
\fB0\fR. Otherwise, \fB\(mi1\fR is returned and \fBerrno\fR is set to indicate
the error.
.SH ERRORS
.sp
.LP
The \fBftruncate()\fR and \fBtruncate()\fR functions will fail if:
.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 19n
A signal was caught during execution.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 19n
The \fIlength\fR argument was less than 0.
.RE

.sp
.ne 2
.na
\fB\fBEFBIG\fR or \fBEINVAL\fR\fR
.ad
.RS 19n
The \fIlength\fR argument was greater than the maximum file size.
.RE

.sp
.ne 2
.na
\fB\fBEIO\fR\fR
.ad
.RS 19n
An I/O error occurred while reading from or writing to a file system.
.RE

.sp
.ne 2
.na
\fB\fBEROFS\fR\fR
.ad
.RS 19n
The named file resides on a read-only file system.
.RE

.sp
.LP
The \fBtruncate()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 16n
A component of the path prefix denies search permission, or write permission is
denied on the file.
.RE

.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 16n
The \fIpath\fR argument points outside the process' allocated address space.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 16n
The \fIpath\fR argument is not an ordinary file.
.RE

.sp
.ne 2
.na
\fB\fBEISDIR\fR\fR
.ad
.RS 16n
The named file is a directory.
.RE

.sp
.ne 2
.na
\fB\fBELOOP\fR\fR
.ad
.RS 16n
Too many symbolic links were encountered in resolving \fIpath\fR.
.RE

.sp
.ne 2
.na
\fB\fBEMFILE\fR\fR
.ad
.RS 16n
The maximum number of file descriptors available to the process has been
reached.
.RE

.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
The length of the specified pathname exceeds {\fBPATH_MAX\fR} bytes, or the
length of a component of the pathname exceeds {\fBNAME_MAX\fR} bytes.
.RE

.sp
.ne 2
.na
\fB\fBENOENT\fR\fR
.ad
.RS 16n
A component of \fIpath\fR does not name an existing file or \fIpath\fR is an
empty string.
.RE

.sp
.ne 2
.na
\fB\fBENFILE\fR\fR
.ad
.RS 16n
Additional space could not be allocated for the system file table.
.RE

.sp
.ne 2
.na
\fB\fBENOTDIR\fR\fR
.ad
.RS 16n
A component of the path prefix of \fIpath\fR is not a directory.
.RE

.sp
.ne 2
.na
\fB\fBENOLINK\fR\fR
.ad
.RS 16n
The \fIpath\fR argument points to a remote machine and the link to that machine
is no longer active.
.RE

.sp
.LP
The \fBftruncate()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEAGAIN\fR\fR
.ad
.RS 19n
The file exists, mandatory file/record locking is set, and there are
outstanding record locks on the file (see \fBchmod\fR(2)).
.RE

.sp
.ne 2
.na
\fB\fBEBADF\fR or \fBEINVAL\fR\fR
.ad
.RS 19n
The \fIfildes\fR argument is not a file descriptor open for writing.
.RE

.sp
.ne 2
.na
\fB\fBEFBIG\fR\fR
.ad
.RS 19n
The file is a regular file and \fIlength\fR is greater than the offset maximum
established in the open file description associated with \fIfildes\fR.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 19n
The \fIfildes\fR argument references a file that was opened without write
permission.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 19n
The \fIfildes\fR argument does not correspond to an ordinary file.
.RE

.sp
.ne 2
.na
\fB\fBENOLINK\fR\fR
.ad
.RS 19n
The \fIfildes\fR argument points to a remote machine and the link to that
machine is no longer active.
.RE

.sp
.LP
The \fBtruncate()\fR function may fail if:
.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
Pathname resolution of a symbolic link produced an intermediate result whose
length exceeds {\fBPATH_MAX\fR}.
.RE

.SH USAGE
.sp
.LP
The \fBtruncate()\fR and \fBftruncate()\fR functions have transitional
interfaces for 64-bit file offsets.  See \fBlf64\fR(7).
.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Standard
_
MT-Level	MT-Safe
.TE

.SH SEE ALSO
.sp
.LP
.BR chmod (2),
.BR fcntl (2),
.BR open (2),
.BR attributes (7),
.BR lf64 (7),
.BR standards (7)
